package com.project.service.ipml;

import com.project.bean.LogBean;
import com.project.bean.UserBean;
import com.project.mapper.IUserMapper;
import com.project.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Calendar;
import java.util.List;
@Service
@Transactional
public class UserServiceImpl implements IUserService {
    @Autowired
    private IUserMapper mapper;

    @Override
    public UserBean login(String userName, String pwd) {
        UserBean userBean = mapper.login(userName, pwd);
        if (userBean != null) {
            Calendar cal = Calendar.getInstance();
            String content= "用户" + userName + "在" + cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE) + "登录系统";
            LogBean logBean = new LogBean(content);
            mapper.addJournal(logBean);
            return userBean;
        }
        return null;
    }

    @Override
    public void updatePwd(Integer id, String newPwd) {
        mapper.updatePwd(id, newPwd);
    }

    @Override
    public boolean add(UserBean userBean) {
        int count = mapper.countUserName(userBean.getUserName());
        if (count == 0) {
            mapper.add(userBean);
            return true;
        } else {
            return false;
        }

    }

    @Override
    public void del(Integer id) {
        mapper.del(id);
    }

    @Override
    public UserBean findById(Integer id) {
        return mapper.findById(id);
    }

    @Override
    public void updateGrade(Integer id, String newGrade) {
        mapper.updateGrade(id, newGrade);
    }

    @Override
    public List<UserBean> findByItem(String grade) {
        return mapper.findByItem(grade);
    }
}
